package com.binge.restTest;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

/**
 * ClassName:RestTest1
 * Package:com.binge.restTest
 * Description:
 *
 * @Author @Bin
 * @Date 2025/1/14-19:11
 * @Version: v1.0
 */
@Slf4j
public class RestTest1 {

    @Test
    public void testCert() throws IOException, KeyManagementException {
        System.setProperty("javax.net.debug", "all");
        // 创建自定义的SSLSocketFactory来打印详细的握手信息
        SSLContext context = null;
        try {
            context = SSLContext.getInstance("TLSv1.2");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        context.init(null, new TrustManager[] {
                new X509TrustManager() {
                    @Override
                    public X509Certificate[] getAcceptedIssuers() { return null; }
                    @Override
                    public void checkClientTrusted(X509Certificate[] certs, String authType) { }
                    @Override
                    public void checkServerTrusted(X509Certificate[] certs, String authType) {
                        System.out.println("收到服务器证书:");
                        for (X509Certificate cert : certs) {
                            System.out.println("Subject: " + cert.getSubjectDN());
                            System.out.println("Issuer: " + cert.getIssuerDN());
                        }
                    }
                }
        }, new SecureRandom());

        URL url = new URL("https://cmeeting.cnooc:7443/api/rest/v6/contact/load/enterpriseType");
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setSSLSocketFactory(context.getSocketFactory());
        conn.connect();
    }
}